package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.Hycy;
import cn.stylefeng.guns.sys.modular.system.model.params.HycyParam;
import cn.stylefeng.guns.sys.modular.system.service.HycyService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * 控制器
 *
 * @author jiangge
 * @Date 2020-10-28 15:54:15
 */
@Controller
@RequestMapping("/hycy")
public class HycyController extends BaseController {

    private String PREFIX = "/modular/system/hycy";

    @Autowired
    private HycyService hycyService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/hycy.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hycy_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hycy_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HycyParam hycyParam) {
        this.hycyService.add(hycyParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HycyParam hycyParam) {
        this.hycyService.update(hycyParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HycyParam hycyParam) {
        this.hycyService.delete(hycyParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HycyParam hycyParam) {
        Hycy detail = this.hycyService.getById(hycyParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-10-28
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HycyParam hycyParam) {
        return this.hycyService.findPageBySpec(hycyParam);
    }



    /**
     * 会员卡信息导入
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object ddpx(@RequestParam("file") MultipartFile file,@RequestParam(value = "dataTime", required = false)String dataTime) {

        if(!dataTime.equals("")){
            String fileName = file.getOriginalFilename();
            JsonResult jsonResult = new JsonResult();
            int result;
            try {
                result = this.hycyService.batchImport(fileName, file,dataTime);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult.setState(0);
                jsonResult.setErrmsg(e.getMessage());
                return jsonResult;
            }
            return result;
        }else {
            return 3;
        }

    }

}


